package gui.reader;

import dao.SQLiteConnection;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;

public class ReaderSelectWin extends JFrame {
    JPanel upper,middle,lower;
    JLabel l1,l2,l3,l4,user;
    JTextField t1,t2,t3,t4;
    JButton select,empty;
    JButton register,login;
    JTable table;
    DefaultTableModel model;
    String usrname;


    public ReaderSelectWin(String a){
        usrname=a;
        init();
        setTitle("读者查阅");
        setSize(1000, 600);
        setLocationRelativeTo(null);    //窗口居中
        setVisible(true);
        setResizable(false);    //禁用用户调整界面大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //终止应用运行
    }

    private void init() {

        //搜索框实现在顶部
        upper=new JPanel(new FlowLayout());

        l1=new JLabel("编号：");
        t1=new JTextField(15);
        upper.add(l1);
        upper.add(t1);

        l2=new JLabel("书名：");
        t2=new JTextField(15);
        upper.add(l2);
        upper.add(t2);

        l3=new JLabel("分类：");
        t3=new JTextField(15);
        upper.add(l3);
        upper.add(t3);

        l4=new JLabel("作者：");
        t4=new JTextField(15);
        upper.add(l4);
        upper.add(t4);

        select=new JButton("查询");
        empty=new JButton("清空");
        upper.add(select);
        upper.add(empty);

        //图书信息表展示在中部
        middle=new JPanel();
        //图书信息表
        table = new JTable();
        model = new DefaultTableModel();  //表格模型


//        table.setEnabled(false);    //表格只读
        table.setRowHeight(20);// 设置表格行宽
        table.getTableHeader().setReorderingAllowed(false);  //表格列禁止拖动
        table.setPreferredScrollableViewportSize(new Dimension(965, 420)); // 设置表格宽度和高度

        // 创建表格列名 添加表头
        String[] columnNames = {"ISBN","书名","分类","出版社","作者","馆藏"};
        model.setColumnIdentifiers(columnNames);

        // 将表格组件放在滚动面板中
        JScrollPane scrollPane = new JScrollPane(table);
//        middle.add(scrollPane);
        //连接数据库
        SQLiteConnection connection=new SQLiteConnection();
        connection.Connect_SQLite();
        // 读取数据库中的数据
        try {
            Connection conn = connection.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM book");
            ResultSetMetaData meta = rs.getMetaData();
            int columnCount = meta.getColumnCount();
            // 添加数据
            while (rs.next()) {
                Object[] rowData = new Object[columnCount];
                for (int i = 1; i <= columnCount; i++) {
                    rowData[i - 1] = rs.getObject(i);
                }
                model.addRow(rowData);
            }
            table.setModel(model);    // 设置表格的数据和显示
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        connection.closeConnection();
        middle.add(scrollPane);


        lower=new JPanel();
        user=new JLabel("欢迎登录"+usrname);
//        register=new JButton("注册");
        login=new JButton("借阅历史");
        lower.add(user);
//        lower.add(register);
        lower.add(login);

        add(upper,BorderLayout.NORTH);
        add(middle);
        add(lower,BorderLayout.SOUTH);

    }
    public JTextField getT1() {
        return t1;
    }
    public JTextField getT2() {
        return t2;
    }
    public JTextField getT3() {
        return t3;
    }
    public JTextField getT4() {
        return t4;
    }
    public JButton getSelect() {
        return select;
    }
    public JButton getEmpty() {
        return empty;
    }
    public JButton getRegister() {
        return register;
    }
    public JButton getLogin() {
        return login;
    }
    public JTable getTable() {
        return table;
    }
    public DefaultTableModel getModel() {
        return model;
    }
}
